#include "stdafx.h"
#include "Model.h"
#include "Utils.h"

cModel::cModel(): m_ID(-1), m_aVertices(NULL), m_aIndices(NULL),
		m_iNumVertices(0), m_iNumIndices(0), m_iNumFaces(0)
{
	memset(m_fileModel, '\0', MAX_LENGTH_NAME);
}

cModel::cModel(int id, const char *fileModel)
{
	cModel();
	m_ID = id;

	int i = 0;
	while (fileModel[i] != '\0')
	{
		m_fileModel[i] = fileModel[i];
		i++;
	}
}

cModel::~cModel()
{
	SAFE_DEL_ARRAY(m_aVertices);
	SAFE_DEL_ARRAY(m_aIndices);
	glDeleteBuffers(2, m_vboIds);
}

void cModel::Init()
{
	if (m_iNumVertices > 0) //already init
	{
		return;
	}

	FILE *pFile = NULL;
	char *fm_url = JoinString(MODELS_PATH, m_fileModel);
	char buffer[MAX_LENGTH];

	fopen_s (&pFile, fm_url, "r");
	SAFE_DEL_ARRAY(fm_url);

	// Read number of vertices
	ReadLine(pFile, buffer);
	int iPos = ScanNumberInt32(buffer, &m_iNumVertices, 0);
	m_aVertices = new Vertex[m_iNumVertices];

	// Read vertices data
	int iIndex;
	int iID;
	for (iIndex = 0; iIndex < m_iNumVertices; ++iIndex)
	{
		ReadLine(pFile, buffer);
		
		// ID
		iPos = ScanNumberInt32(buffer, &iID, 0); 

		// Position
		iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].position.x, iPos); 
		iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].position.y, iPos); 
		iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].position.z, iPos); 

		// Normal
		iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].normal.x, iPos); 
		iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].normal.y, iPos); 
		iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].normal.z, iPos); 

		// Binormal
		iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].bitangent.x, iPos); 
		iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].bitangent.y, iPos); 
		iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].bitangent.z, iPos); 

		// Tangent
		iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].tangent.x, iPos); 
		iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].tangent.y, iPos); 
		iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].tangent.z, iPos);

		// Texcoord
		iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].uv.x, iPos); 
		iPos = ScanNumberFloat32(buffer, &m_aVertices[iIndex].uv.y, iPos); 
	}

	//for (iIndex = 0; iIndex < m_iNumVertices; ++iIndex)
	//	LOGI("%f %f %f\n", m_aVertices[iIndex].position.x, m_aVertices[iIndex].position.y, m_aVertices[iIndex].position.z);

	// Read number of Indices
	ReadLine(pFile, buffer);
	ScanNumberInt32(buffer, &m_iNumIndices);

	m_aIndices = new unsigned int[m_iNumIndices];
	m_iNumFaces = m_iNumIndices/3;

	// Read Indices data
	for (iIndex = 0; iIndex < m_iNumFaces; ++iIndex)
	{
		ReadLine(pFile, buffer);
		
		// ID
		iPos = ScanNumberInt32(buffer, &iID, 0); 

		iPos = ScanNumberUInt32(buffer, &m_aIndices[iIndex* 3]    , iPos); 
		iPos = ScanNumberUInt32(buffer, &m_aIndices[iIndex* 3 + 1], iPos); 
		iPos = ScanNumberUInt32(buffer, &m_aIndices[iIndex* 3 + 2], iPos);
	}

	//for (iIndex = 0; iIndex < m_iNumFaces; ++iIndex)
	//	LOGI("%d %d %d\n", m_aIndices[iIndex* 3], m_aIndices[iIndex* 3 + 1], m_aIndices[iIndex* 3 + 2]);

	fclose (pFile);


	//Init VBOs
    glGenBuffers(2, m_vboIds);

    glBindBuffer(GL_ARRAY_BUFFER, m_vboIds[0]);
    glBufferData(GL_ARRAY_BUFFER, m_iNumVertices * sizeof(Vertex), m_aVertices, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vboIds[1]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_iNumIndices * sizeof(unsigned int), m_aIndices, GL_STATIC_DRAW);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
